# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="MrEction"
__date__ ="$Mar 9, 2011 11:55:43 AM$"

from hexconfig import *
#from actor import Actor
from pygame.draw import *
from actor import *

class Unit():
    def __init__(self,team = 0):
        self.hexloc = [0,0,0]
        self.team = team
        self.state = IDLE
        self.drawLerpMode = False
        self.drawLoc = [0.0,0.0]
        self.dead = False
        self.king = False
        self.dazed = False

        self.activeDelays = []
        self.activeFrame = 0

        self.unit_id = 0    #possibly make this  a construction variable
        #UNIT_ID_TAG+=1

    def update(self,etime):
        if self.state == IDLE:
            return

        print "DOING CRAZY SHIT, just lerping when commanded"
        return

    def draw(self,surf):
        #print "HELL"
        if not self.drawLerpMode and not self.dead:
            drawLoc = getPosFromHexLoc(self.hexloc,True)
        else:
            drawLoc = self.drawLoc

        circle(surf,(0,0,0),drawLoc,16)

        if self.team == 0:
            circle(surf,P0UNITCOLOR,drawLoc,12)
        else:
            circle(surf,P1UNITCOLOR,drawLoc,12)

    def drawDead(self,surf,drawLoc):
        circle(surf,(0,0,0),drawLoc,16)

        if self.team == 0:
            circle(surf,P0UNITCOLOR,drawLoc,12)
        else:
            circle(surf,P1UNITCOLOR,drawLoc,12)



class SpriteUnit(Unit,Actor):
    def __init__(self,team = 0,king = False, location=(0,0,0)):
        if king:
            if team == 0:
                Actor.__init__(self,pygame.image.load("../images/"+QUEEN0SHEET),(32,32),location)
                self.facingLeft = False
            else:
                Actor.__init__(self,pygame.image.load("../images/"+QUEEN1SHEET),(32,32),location)
        else:
            if team == 0:
                Actor.__init__(self,pygame.transform.scale2x(pygame.image.load("../images/"+TEAM0SHEET)),(32,32),location)
                self.facingLeft = False
            else:
                Actor.__init__(self,pygame.transform.scale2x(pygame.image.load("../images/"+TEAM1SHEET)),(32,32),location)

        Unit.__init__(self,team)


        self.setLoc(location)
        #self.loc = [float(self.colRect.center[0]),float(self.colRect.center[1])]
        self.jumping = False
        self.velUp = True
        self.king = king
        self.anim_etime = 0.0
        #self.drawDisp = [[-3,-6],[-3,-6],[0,-6],[-3,-3]]
        self.drawDisp = [[-6,-12],[-6,-12],[0,-10],[-6,-6]]

        self.runAnimSpd = 0.0

        self.img = pygame.Surface((44,44))
        self.img.set_colorkey(COLORKEY)
        self.imgState = 0
        #self.img = self.img.convert()
        self.imgSize = (44,44)#this is being reset after (16,16)gets sent into actor
                                #this is so that the colRect is still based off (16,16)
        self.img.blit(self.imgSheet,(0,0),((self.imgFrame*44,\
                      self.imgState*44),self.imgSize))

        self.theta = 0.0

        self.lightningBolt = pygame.image.load("../imgs/units/lightningBolt.png")
        self.lightningBolt.set_colorkey((255,255,255))



        #simple action flag
        self.hasTakenAction = False

        self.active = False
        self.activeTime = 0.0
        self.activeFrame = 0
        self.rot = 0.0
    def update(self,etime):
        if self.dead: return
        if self.active:
            self.activeTime += etime
            if self.activeTime > ACTIVE_ANIM_FRAME_LIMIT[self.activeFrame]:
                self.activeTime = self.activeTime%ACTIVE_ANIM_FRAME_LIMIT[self.activeFrame]
                self.activeFrame = (self.activeFrame+1) % 2

    def setActive(self):
        self.active = True
        self.activeTime = 0.0
        self.activeFrame = 0

    def endActive(self):
        self.active = False
        self.activeTime = 0.0
        self.activeFrame = 0
        self.setImgIndex(0, 0)
    
    def draw(self,surf):
        
            
        if self.imgState == 3 and not self.jumping:
            self.setImgIndex(0)

        if self.imgState == 0 and self.active:
            self.setImgIndex(0,self.activeFrame)
        
        self.colRect.center = getPosFromHexLoc(self.hexloc) if not self.drawLerpMode else self.drawLoc
        Actor.draw(self,surf)

        if self.dazed and not self.dead:
            drawLoc = self.lightningBolt.get_rect(center = self.colRect.midtop).topleft
            drawLoc = [drawLoc[0],drawLoc[1]]
            if random.random()<0.1:
                drawLoc[random.randrange(2)]+=random.randrange(-2,2)

            surf.blit(self.lightningBolt,drawLoc)
            
    def setLoc(self,loc):
        self.hexloc = loc
        self.colRect.center = getPosFromHexLoc(self.hexloc)
    

if __name__ == "__main__":
    x = "blar'glgl' ah!"
    print x
